package com.itheima.security;

import com.itheima.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class SpringSecurityUserService implements UserDetailsService {

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    //使用一个map，将其模拟为一个数据库
    private Map map = new HashMap<>();

    public void init(){

        User user1 = new User();
        user1.setUsername("zhangsan");
        user1.setPassword(bCryptPasswordEncoder.encode("1234"));

        User user2 = new User();
        user2.setUsername("lisi");
        user2.setPassword(bCryptPasswordEncoder.encode("1234"));

        map.put(user1.getUsername(),user1);
        map.put(user2.getUsername(),user2);

    }

    /**
     * 该方法会在提交登陆信息是执行
     * @param username ：用户提交的用户名称
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //初始化数据库
        init();

        //模拟从数据库通过用户名查询数据
        User user = (User) map.get(username);

        //给用户设置权限或者角色
        List<GrantedAuthority> list = new ArrayList<>();
        /**
         * 设置角色的规则：
         *      全单词大写：角色名称添加"ROLE_"前缀,比如给用户添加的一个admin角色，ROLE_ADMIN
         *
         * 设置权限的规则：
         *      没有大小写限制，权限不需要添加任何前缀。add\ADD
         *
         */
        /*list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        list.add(new SimpleGrantedAuthority("add"));
*/
        if (username.equals("zhangsan")){
            list.add(new SimpleGrantedAuthority("add"));
            list.add(new SimpleGrantedAuthority("ROLE_MANAGER"));
        }
        if (username.equals("lisi")){
            list.add(new SimpleGrantedAuthority("delete"));
            list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        }

        //如果密码没有加密，是一个明文密码，需要在密码前面添加"{noop}"
        return new org.springframework.security.core.userdetails.User(user.getUsername(),user.getPassword(),list);
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode("1234");
        String encode1 = passwordEncoder.encode("1234");
        System.out.println(encode);
        System.out.println(encode1);

        boolean flag1 = passwordEncoder.matches("1234", encode);
        boolean flag2 = passwordEncoder.matches("1234", encode1);

        System.out.println(flag1);
        System.out.println(flag2);


    }
}
